package com.hermit.qmst.UI.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.hermit.qmst.R;
import com.hermit.qmst.UI.XlistView.XListView;
import com.hermit.qmst.adapter.AuthorityAdapter;
import com.hermit.qmst.mode.MessageInfo;
import com.hermit.qmst.tools.Common;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 官方动态
 * Created by Administrator on 2016/10/15 0015.
 */
public class AuthorityActivity extends Activity implements XListView.IXListViewListener {
    private String TAG = "官方动态:AuthorityActivity_";
    private XListView xListView;
    private AuthorityAdapter adapter;
    private int pageIndex = 0;
    private ProgressDialog mProgressDialog;

    //    private
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authority);
        adapter = new AuthorityAdapter(AuthorityActivity.this);
        xListView = (XListView) findViewById(R.id.xListView);
        xListView.setAdapter(adapter);
        xListView.setAdapter(adapter);
        xListView.setXListViewListener(this);
        xListView.setPullLoadEnable(false);
    }

    private void onLoad() {
        xListView.stopRefresh();
        xListView.stopLoadMore();
        xListView.setRefreshTime("刚刚");
    }

    @Override
    public void onLoadMore() {
        pageIndex++;
        getData();
    }

    @Override
    public void onRefresh() {
        pageIndex = 0;
        getData();
        adapter.data.clear();
    }

    private void getData() {
        mProgressDialog = ProgressDialog.show(this, "提示", "系统正在处理，请稍后…", false, false);
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url = Common.submitAuthority() + Common.mAgentId + "&curPage=" + pageIndex;
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject object) {
                try {

                    if (object.getBoolean("state")) {
                        JSONArray jsonArray = object.getJSONArray("array");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            MessageInfo info = new MessageInfo();
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            info.setTitle(jsonObject.getString("title"));
                            info.setId(jsonObject.getString("id"));
                            info.setContent(jsonObject.getString("content"));
                            info.setCreatetime(jsonObject.getString("createtime"));
                            adapter.data.add(info);
                        }
                        if (jsonArray.length() > Common.indexCount) {
                            xListView.setPullLoadEnable(true);
                        }
                        adapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    onLoad();
                    mProgressDialog.dismiss();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                onLoad();
                mProgressDialog.dismiss();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }
}
